<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');

class AdController extends AdminbaseController{
    
	protected $ad_model;
	
	public function _initialize() {
		parent::_initialize();
		$this->ad_model = D("Common/Ad");
	}
	
	// 后台广告列表
	public function index(){
		$where=array();
    	$request=I('request.');
    	if(!empty($request['ad_name'])){
    		$where['ad_name']=$request['ad_name'];
    		$this->assign("ad_name",$request['ad_name']);
    	}
    	if(!empty($request['status'])){
    		$where['status']=$request['status'];
    		$this->assign("status",$request['status']);
    	}
    	if(!empty($request['classid'])){
    		$where['classid']=$request['classid'];
    		$this->assign("classid",$request['classid']);
    	}
    	$start_time=I('request.start_time');
    	if(!empty($start_time)){
    		$where['create_time'][0]=array(
    				array('EGT',$start_time)
    		);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    		$where['create_time'][1]=array(
    				array('ELT',$end_time)
    		);
    	}
    	$count = $this->ad_model->where($where)->count();
		$size = $page = $this->page($count, 20);
		
		$ads=$this->ad_model->where($where)->order("series ASC,create_time DESC")
		->limit($page->firstRow, $page->listRows)
		->select();
		
		if(!empty($ads) && is_array($ads)){
			$existdata=true;
		}else{
			$existdata=false;
		}
		$this->assign("page", $page->show('Admin'));
		$this->assign("ads",$ads);
		$this->assign("existdata", $existdata);
		$this->display();
	}
	
	// 广告添加
	public function add(){
		$this->display();
	}
	
	// 广告添加提交
	public function add_post(){
		if(IS_POST){
			if(empty($_POST['frm']['classid'])){
    			$this->error("请选择一个分类！");
    		}
    		if(!empty($_POST['frm']['thumb'])){
    			$_POST['frm']['thumb'] = sp_asset_relative_url($_POST['frm']['thumb']);
    		}
    		if(empty($_POST['frm']['ad_name'])){
    			$this->error("广告名称不能为空！");
    		}
    		if(empty($_POST['frm']['ad_ident'])){
    			$this->error("广告标识不能为空！");
    		}
    		$data = I("post.frm");
			$data['UserID'] = get_current_admin_id();
			$data['IssueIP'] = get_real_ip();
			if ($this->ad_model->add($data)!==false) {
				$this->success("添加成功",U("Ad/index"));
			} else {
				$this->error("添加失败！");
			}
		}
	}
	
	// 广告编辑
	public function edit(){
		$id=I("get.id",0,'intval');
		$ad=$this->ad_model->where(array('ad_id'=>$id))->find();
		$this->assign('ads',$ad);
		$this->display();
	}
	
	// 广告编辑提交
	public function edit_post(){
		if (IS_POST) {
			$id = I("post.id",0,"intval");
			$data = I("post.frm");
			$data['LastModifyIP'] = get_real_ip();
			$data['update_time'] = date("Y-m-d H:i:s",time());
			if ($this->ad_model->where(array("ad_id"=>$id))->save($data)!==false) {
				$this->success("保存成功！", U("ad/index"));
			} else {
				$this->error("保存失败！");
			}
		}
	}
	
	// 广告删除
	public function delete(){
		$id = I("get.id",0,"intval");
		if ($this->ad_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	// 广告显示/隐藏
	public function toggle(){
		if(!empty($_POST['ids']) && isset($_GET["display"])){
			$ids = I('post.ids/a');
			if ($this->ad_model->where(array('ad_id'=>array('in',$ids)))->save(array('status'=>1))!==false) {
				$this->success("启用成功！");
			} else {
				$this->error("启用失败！");
			}
		}
		
		if(isset($_POST['ids']) && isset($_GET["hide"])){
			$ids = I('post.ids/a');
			if ($this->ad_model->where(array('ad_id'=>array('in',$ids)))->save(array('status'=>2))!==false) {
				$this->success("禁用成功！");
			} else {
				$this->error("禁用失败！");
			}
		}
	}
	public function adorders(){//广告排序
		$status = parent::_listorders($this->ad_model,"series","ad_id");
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
}